﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.Web.Mvc;

namespace Mvc.HelpDesk
{
    public class XmlResult : ActionResult
    {
        private object _objectToSerialize;
        private string _fileName;

        public XmlResult(object objectToSerialize, string fileName)
        {
            _objectToSerialize = objectToSerialize;
            _fileName = fileName;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (_objectToSerialize != null)
            {
                var xs = new XmlSerializer(_objectToSerialize.GetType());
               
                context.HttpContext.Response.ContentType = "application/octet-stream";
                context.HttpContext.Response.AddHeader("Content-Disposition:", "filename="+_fileName);
                xs.Serialize(context.HttpContext.Response.Output, _objectToSerialize);
            }
        }
    }
}